import 'package:flutter/material.dart';
import 'dart:math';

import '../model/orderVO.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

class CircleWidget extends StatelessWidget {
  final int numberOfSegments;
  final int activeSegments;
  final int orderState;

  const CircleWidget({super.key,
    required this.numberOfSegments,
    required this.activeSegments, required this.orderState,
  });

  @override
  Widget build(BuildContext context) {

    Color c;
    orderState == OrderState.WAITING?
        c = Colors.orange
    :orderState == OrderState.FINISHED?
    c = Colors.green
    :c=const Color(0xfff07167);

    return Container(
      margin:   EdgeInsets.only(bottom: 18.h),
      child: CustomPaint(
        // size: const Size(50, 50),
        painter: CirclePainter(
          numberOfSegments: numberOfSegments,
          activeSegments: activeSegments,
          orderState:orderState,
        ),
        child: Padding(
          padding:   EdgeInsets.only(left: 8.0.w,right: 8.w),
          child:
          orderState == OrderState.WAITING?
          Text('未抄表',style:TextStyle(fontSize: 18.sp,color:c,fontWeight: FontWeight.w900) ,)
          :orderState == OrderState.FINISHED?
           Text('已抄表',style:TextStyle(fontSize: 18.sp,color:c,fontWeight: FontWeight.w900) ,)
          : Text('已出单',style:TextStyle(fontSize: 18.sp,color:c,fontWeight: FontWeight.w900) ,),
        ),
      ),
    );
  }
}

class CirclePainter extends CustomPainter {
  final int numberOfSegments;
  final int activeSegments;
  final int orderState;

  CirclePainter(  {
    required this.numberOfSegments,
    required this.activeSegments,
    required this.orderState,
  });

  @override
  void paint(Canvas canvas, Size size) {
    double strokeWidth = 7.0.w;
    Color baseColor = Colors.grey;
    Color activeColor = Colors.blue;

    double radius = size.width / 2;
    Offset center = Offset(size.width / 2, size.height / 2);

    // 画背景圆形
    Color c = Colors.white;
    // orderState == OrderState.WAITING?
    //     c = Colors.orange
    // :orderState == OrderState.FINISHED?
    // c = Colors.green
    // :c=const Color(0xfff07167);

    Paint backgroundPaint = Paint()
      ..color = c // 背景颜色
      ..style = PaintingStyle.fill;

    canvas.drawCircle(center, radius, backgroundPaint);

    // 画每个圆环
    for (int i = 0; i < numberOfSegments; i++) {
      bool isActive = i < activeSegments;
      Paint paint = Paint()
        ..color = isActive ? activeColor : baseColor
        ..style = PaintingStyle.stroke
        ..strokeWidth = strokeWidth;

      double startAngle = (i * 2 * pi) / numberOfSegments - (pi / 2);
      double sweepAngle = (2 * pi) / numberOfSegments;

      canvas.drawArc(
        Rect.fromCircle(center: center, radius: radius - strokeWidth / 2),
        startAngle,
        sweepAngle,
        false,
        paint,
      );
    }
  }

  @override
  bool shouldRepaint(CustomPainter oldDelegate) {
    return true;
  }
}
